//
// Created by hrg on 2019/12/5.
//
#include <ecdriver.h>
#include <device.h>
#include <cassert>

EcDriver::EcDriver(EcData* ecdata, int mid)
{
    m_Device_ = new Device(ecdata, mid);
}

EcDriver::~EcDriver()
{
    if(m_Device_)
    {
        delete m_Device_;
        m_Device_ = NULL;
    }
}

bool EcDriver::Init(EcParam ecParam, std::string xmldir)
{
    return m_Device_->Init(ecParam.m_Period_, ecParam.m_Offset_, ecParam.m_Mode_, xmldir);
}

bool EcDriver::Config()
{
    return m_Device_->Config();
}

bool EcDriver::StartRTask()
{
    if(!m_Device_->StartRTask())
    {
        return false;
    }
    return true;
}

bool EcDriver::StopRTask()
{
    return m_Device_->StopRTask();
}

uint16_t EcDriver::GetErrorCode(int index)
{
    return m_Device_->GetErrorCode(index);
}

void EcDriver::SetCurstomInterface(boost::function<Target(EcStatus&)> handle)
{
    m_Device_->SetCurstomInterface(handle);
}

int EcDriver::GetServoNum()
{
    return m_Device_->GetServoNum();
}

int EcDriver::GetEcIONum()
{
    return m_Device_->GetEcIONum();
}

EcType EcDriver::GetSlvType(int index)
{
    return m_Device_->GetSlvType(index);
}

int8_t EcDriver::GetOneStatus(int index)
{
    return m_Device_->GetOneStatus(index);
}

bool EcDriver::SetIOCmd(int index, int cmd)
{
    return m_Device_->SetIOCmd(index, cmd);
}

#if 0
uint32_t EcDriver::GetIOCmd(int index){
    assert(m_Device_ != NULL);
    return m_Device_->GetIOCmd(index);
}
#endif

uint32_t EcDriver::GetIOAna(int index, int pdo)
{
    return m_Device_->GetIOAna(index, pdo);
}

uint32_t EcDriver::GetIODig(int index)
{
    return m_Device_->GetIODig(index);
}

void EcDriver::AllReset()
{
    for(int i = 0; i < GetServoNum(); ++ i)
    {
        Reset(i);
    }
}

void EcDriver::Reset(int index)
{
    m_Device_->Reset(index);
}

void EcDriver::ClearDataCache()
{
    m_Device_->ClearDataCache();
}

int* EcDriver::GetCurrPos()
{
    return m_Device_->GetCurrPos();
}

int* EcDriver::GetCurrVel()
{
    return m_Device_->GetCurrVel();
}

int* EcDriver::GetTargetPos()
{
    return m_Device_->GetTargetPos();
}

int* EcDriver::GetTargetVel()
{
    return m_Device_->GetTargetVel();
}

int8_t EcDriver::GetDeviceStatus()
{
    return m_Device_->GetAllStatus();
}

bool EcDriver::EnableSlaves(int index)
{
    return m_Device_->EnableSlaves(index);
}

bool EcDriver::DisableSlaves(int index)
{
    return m_Device_->DisableSlaves(index);
}

bool EcDriver::IsEnableSlaveCheck(int index)
{
    return m_Device_->IsEnableSlaveCheck(index);
}
